package com.multiple.activities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class CalculatorActivity extends Activity implements OnClickListener {
	/** Called when the activity is first created. */
	int buttonid[] = {R.id.button0, R.id.button1, R.id.button2, R.id.button3, R.id.button4, R.id.button5, R.id.button6,R.id.button7, R.id.button8, R.id.button9};
	Button[] number = new Button[10];
	EditText IO;
	Button decimal, sign, equals, plus, minus, divide, multiply, clear, factor, exponent, change;
	String text="";
	double term, answer;
	String prevop;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.calcmain);
		//Setup # buttons

		for(int i = 0; i < 10; i++)
		{
			number[i] = (Button) findViewById(buttonid[i]);
			number[i].setText(Integer.toString(i));
			number[i].setOnClickListener(this);
		}
		//Setup non-# buttons
		IO = (EditText) findViewById(R.id.IO);
		decimal = (Button) findViewById(R.id.buttondecimal);
		sign = (Button) findViewById(R.id.buttonsign);
		equals = (Button) findViewById(R.id.buttonequals);
		plus = (Button) findViewById(R.id.buttonplus);
		minus = (Button) findViewById(R.id.buttonminus);
		divide = (Button) findViewById(R.id.buttondivide);
		multiply = (Button) findViewById(R.id.buttonmultiply);
		clear = (Button) findViewById(R.id.buttonclear);
		factor = (Button) findViewById(R.id.buttonfactor);
		exponent = (Button) findViewById(R.id.buttonexponent);
		change = (Button) findViewById(R.id.buttonchange);
		
		change.setText("Back");
		change.setOnClickListener(this);
		
		exponent.setText("^");
		exponent.setOnClickListener(this);

		factor.setText("!");
		factor.setOnClickListener(this);

		decimal.setText(".");
		decimal.setOnClickListener(this);

		sign.setText("+/-");
		sign.setOnClickListener(this);

		equals.setText("=");
		equals.setOnClickListener(this);

		plus.setText("+");
		plus.setOnClickListener(this);

		minus.setText("-");
		minus.setOnClickListener(this);

		divide.setText("/");
		divide.setOnClickListener(this);

		multiply.setText("*");
		multiply.setOnClickListener(this);

		clear.setText("Clear");
		clear.setOnClickListener(this);

		IO.setOnClickListener(this);

		//Set Setting to Clear
		text = "";
		IO.setText(text);
		term = 0;
		prevop = "";
		answer = 0;
	}
	//Button Functionality
	public void onClick(View v) {
		if(v.equals(change))
		{
			Intent intents = new Intent(this, MultipleActivitiesActivity.class);
			this.startActivity(intents);
		}
		//Clear Settings
		if(v.equals(clear))
		{
			text = "";
			term = 0;
			IO.setText(text);
			prevop = "";
			answer = 0;
		}
		//Typing #s
		for(int i = 0; i < 10; i++)
		{
			if(v.equals(number[i]))
			{
				text += ((Button) v).getText();
				if(prevop == "")
				{
					IO.setText(text);
				}
				else
				{
					IO.setText(term + "\n" + text);
				}
			}
		}
		//Decimals
		if(v.equals(decimal))
		{
			if(text.contains(".") == false)
			{
				text += ".";
				if(prevop == "")
				{
					IO.setText(text);
				}
				else
				{
					IO.setText(term + "\n" + text);
				}
			}
		}
		//Changing sign
		if(v.equals(sign))
		{
			if(!text.startsWith("-"))
			{
				text = "-" + text;
			}
			else
			{
				text = text.substring(1);
			}
			IO.setText(text);
		}
		//Operations
		if(v.equals(multiply))
		{
			if(text == "")
			{
				Toast.makeText(this, "Enter next term.", Toast.LENGTH_SHORT).show();
			}
			else
			{
				if(prevop != "")
				{
					answer = compute(answer, Double.parseDouble(text));
				}
				if(prevop == "")
				{
					answer = Double.parseDouble(text);
				}
				term = Double.parseDouble(text);
				prevop = "multiplication";
				text = "";
				IO.setText(Double.toString(term) + "\n" + text);
			}
		}
		if(v.equals(divide))
		{
			if(text == "")
			{
				Toast.makeText(this, "Enter next term.", Toast.LENGTH_SHORT).show();
			}
			else
			{
				if(prevop != "")
				{
					answer = compute(answer, Double.parseDouble(text));
				}
				if(prevop == "")
				{
					answer = Double.parseDouble(text);
				}
				term = Double.parseDouble(text);
				prevop = "division";
				text = "";
				IO.setText(Double.toString(term) + "\n" + text);
			}
		}
		if(v.equals(plus))
		{
			if(text == "")
			{
				Toast.makeText(this, "Enter next term.", Toast.LENGTH_SHORT).show();
			}
			else
			{
				if(prevop != "")
				{
					answer = compute(answer, Double.parseDouble(text));
				}
				if(prevop == "")
				{
					answer = Double.parseDouble(text);
				}
				term = Double.parseDouble(text);
				prevop = "addition";
				text = "";
				IO.setText(Double.toString(term) + "\n" + text);
			}
		}
		if(v.equals(minus))
		{
			if(text == "")
			{
				Toast.makeText(this, "Enter next term.", Toast.LENGTH_SHORT).show();
			}
			else
			{
				if(prevop != "")
				{
					answer = compute(answer, Double.parseDouble(text));
				}
				if(prevop == "")
				{
					answer = Double.parseDouble(text);
				}
				term = Double.parseDouble(text);
				prevop = "subtraction";
				text = "";
				IO.setText(Double.toString(term) + "\n" + text);
			}
		}
		if(v.equals(factor))
		{
			if(text == "")
			{
				Toast.makeText(this, "Enter next term.", Toast.LENGTH_SHORT).show();
			}
			else
			{
				if(text.contains("."))
				{
					Toast.makeText(this, "Can only perform with integers.", Toast.LENGTH_SHORT).show();
				}
				else
				{
					double temp = factorial(Double.parseDouble(text));
					text = Double.toString(temp);
				}
				if(prevop == "")
				{
					IO.setText(text);
				}
				else
				{
					IO.setText(term + "\n" + text);
				}
			}
		}
		if(v.equals(exponent))
		{
			if(text == "")
			{
				Toast.makeText(this, "Enter next term.", Toast.LENGTH_SHORT).show();
			}
			else
			{
				if(prevop != "")
				{
					answer = compute(answer, Double.parseDouble(text));
				}
				if(prevop == "")
				{
					answer = Double.parseDouble(text);
				}
				term = Double.parseDouble(text);
				prevop = "exponential";
				text = "";
				IO.setText(Double.toString(term) + "\n" + text);
			}
		}
		//Equals
		if(v.equals(equals))
		{
			if(text == "")
			{
				Toast.makeText(this, "Enter next term.", Toast.LENGTH_SHORT).show();
			}
			else
			{
				answer = compute(term, Double.parseDouble(text));
				term = 0;
				prevop = "";
				text = Double.toString(answer);
				IO.setText(text);
			}
		}
	}
	//The actual math
	public double compute(double a, double b){
		double c = 0;
		if(prevop == "multiplication")
		{
			c = a * b;
		}
		else if(prevop == "addition")
		{
			c = a + b;
		}
		else if(prevop == "subtraction")
		{
			c = a - b;
		}
		else if(prevop == "division")
		{
			c = a / b;
		}
		else if(prevop == "exponential")
		{
			c = 1;
			for(double i = b; i > 0; i--)
			{
				c *= a;
			}
		}
		return c;
	}
	public double factorial(double a){
		for(int i = (int) a - 1; i > 0; i--)
		{
			a *= i;
		}
		return a;
	}
}